package com.atguigu.wordcount.multiplejob;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @ClassName Ex1Mapper
 * @DeScription Ex1Mapper
 * @Author 剧情再美终是戏
 * @Date 2019/11/20 14:51
 * @Version 1.0
 **/

// 输入： 0 atguigu pingping
// 输出： atguigu:a.txt 1
public class Ex1Mapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private Text out_key = new Text();
    private IntWritable out_value = new IntWritable(1);

    private String fileName = "";

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        FileSplit fileSplit = (FileSplit) context.getInputSplit();
        if (null != fileSplit) {
            fileName = fileSplit.getPath().getName();
        }
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String[] split = value.toString().split(" ");

        if (null != split && split.length > 0) {
            for (String str : split) {
                out_key.set(str + ":" + fileName);
                context.write(out_key, out_value);
            }
        }
    }
}
